<?php
/**
 * 系统设置
 * Created by PhpStorm.
 * User: Earl <guo@seosite.cn>
 * Date: 2017/9/27
 * Time: 上午10:14
 * Updatetime:
 * updater：
 */
namespace WAdmin\Controller;

class SysconfigController extends BaseController{

    /**
     * @desc
     * @author Egor<http://gitee.com/seosite>
     * @param $param
     * @return array
     */
    public function index($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);


        $model = M('system_config');

        $info = $model->where(['uid'=>$this->userInfo->id])->find();


        $ret_data['common_config'] = [

            'timezone'  =>  empty($info['timezone'])?'':$info['timezone'],
            'language'  =>  empty($info['language'])?'':$info['language'],
            'refresh_time'  =>  empty($info['refresh_time'])?'':$info['refresh_time'],
            'is_warning'  =>  empty($info['is_warning'])?'':$info['is_warning'],
            'speed'  =>  empty($info['speed'])?'':$info['speed'],
            'temperature'  =>  empty($info['temperature'])?'':$info['temperature'],
            'is_expire'  =>  empty($info['is_expire'])?'':$info['is_expire'],

        ];

        $ret_data['map']    =   [

            'map'   =>  empty($info['map'])?'':$info['map'],
            'map_point'   =>  empty($info['map_point'])?'':$info['map_point'],
            'map_gps_useless'   =>  empty($info['map_gps_useless'])?'':$info['map_gps_useless'],
            'map_location_accuracy'   =>  empty($info['map_location_accuracy'])?'':$info['map_location_accuracy'],
            'map_speed'   =>  empty($info['map_speed'])?'':$info['map_speed'],
            'map_gps_time'   =>  empty($info['map_gps_time'])?'':$info['map_gps_time'],
            'map_warning'   =>  empty($info['map_warning'])?'':$info['map_warning'],
            'map_history'   =>  empty($info['map_history'])?'':$info['map_history'],
            'map_unselect_zd'   =>  empty($info['map_unselect_zd'])?'':$info['map_unselect_zd'],
            'map_unselect_zd_detail'   =>  empty($info['map_unselect_zd_detail'])?'':$info['map_unselect_zd_detail'],
            'map_dj'   =>  empty($info['map_dj'])?'':$info['map_dj'],
        ];


        $ret_data['msg']    =   [
            'msg_style'   =>  empty($info['msg_style'])?'':$info['msg_style'],
            'msg_ws'   =>  empty($info['msg_ws'])?'':$info['msg_ws'],
            'msg_oil'   =>  empty($info['msg_oil'])?'':$info['msg_oil'],
            'msg_dl'   =>  empty($info['msg_dl'])?'':$info['msg_dl'],
            'msg_wl'   =>  empty($info['msg_wl'])?'':$info['msg_wl'],
        ];

        $ret_data['dy']     =   [
            'dy_title'   =>  empty($info['dy_title'])?'':$info['dy_title'],
            'dy_time'   =>  empty($info['dy_time'])?'':$info['dy_time'],
        ];

        return showErrorMsg(L("_OPERATION_SUCCESS_"),0,2,$ret_data);
    }

    /**
     * @desc 保存
     * @param $param
     * @return array
     * @author Egor<http://gitee.com/seosite>
     */
    public function save($param)
    {
        $check = CheckFactoryController::isLogin();
        $check->before($param);

        unset($check);

        $model = M('system_config');

        $info = $model->where(['uid'=>$this->userInfo->id])->find();

        if (empty($info)){
            $insert_data = $param;
            $insert_data['uid'] = $this->userInfo->id;

            $insert_data['timezone'] = empty($insert_data['timezone'])?'':$insert_data['timezone'];
            $insert_data['speed'] = empty($insert_data['speed'])?'':$insert_data['speed'];
            $insert_data['temperature'] = empty($insert_data['temperature'])?'':$insert_data['temperature'];
            $insert_data['is_expire'] = empty($insert_data['is_expire'])?'':$insert_data['is_expire'];
            $insert_data['language'] = empty($insert_data['language'])?'1':$insert_data['language'];
            $insert_data['refresh_time'] = empty($insert_data['refresh_time'])?'10':$insert_data['refresh_time'];
            $insert_data['is_warning'] = empty($insert_data['is_warning'])?'2':$insert_data['is_warning'];
            $insert_data['map_location_accuracy'] = empty($insert_data['map_location_accuracy'])?'':$insert_data['map_location_accuracy'];
            $insert_data['map'] = empty($insert_data['map'])?'':$insert_data['map'];
            $insert_data['map_point'] = empty($insert_data['map_point'])?'':$insert_data['map_point'];
            $insert_data['map_gps_useless'] = empty($insert_data['map_gps_useless'])?'':$insert_data['map_gps_useless'];
            $insert_data['map_speed'] = empty($insert_data['map_speed'])?'':$insert_data['map_speed'];
            $insert_data['map_gps_time'] = empty($insert_data['map_gps_time'])?'':$insert_data['map_gps_time'];
            $insert_data['map_warning'] = empty($insert_data['map_warning'])?'':$insert_data['map_warning'];
            $insert_data['map_history'] = empty($insert_data['map_history'])?'':$insert_data['map_history'];
            $insert_data['map_unselect_zd'] = empty($insert_data['map_unselect_zd'])?'':$insert_data['map_unselect_zd'];
            $insert_data['map_unselect_zd_detail'] = empty($insert_data['map_unselect_zd_detail'])?'':$insert_data['map_unselect_zd_detail'];
            $insert_data['map_dj'] = empty($insert_data['map_dj'])?'':$insert_data['map_dj'];
            $insert_data['msg_style'] = empty($insert_data['msg_style'])?'':$insert_data['msg_style'];
            $insert_data['msg_ws'] = empty($insert_data['msg_ws'])?'':$insert_data['msg_ws'];
            $insert_data['msg_oil'] = empty($insert_data['msg_oil'])?'':$insert_data['msg_oil'];
            $insert_data['msg_dl'] = empty($insert_data['msg_dl'])?'':$insert_data['msg_dl'];
            $insert_data['msg_wl'] = empty($insert_data['msg_wl'])?'':$insert_data['msg_wl'];
            $insert_data['dy_title'] = empty($insert_data['dy_title'])?'':$insert_data['dy_title'];
            $insert_data['dy_time'] = empty($insert_data['dy_time'])?'':$insert_data['dy_time'];

            $operation = $model->add($insert_data);

        }else{

            $save_data = $param;

            $save_data['id'] = $info['id'];

            $model->save($save_data);
        }


        return showErrorMsg(L("_OPERATION_SUCCESS_"));
    }
}